﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    public class DocGiaManager
    {
        public ActionResultAsList<DocGia> LayDSDG()
        {
            var ar = new ActionResultAsList<DocGia>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    ar.ObjectList = db.DocGias.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResult Them(ref DocGia dg)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    db.DocGias.Add(dg);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }
        public ActionResult Sua(DocGia dg)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    var docgia = db.DocGias.Single(s => s.MaDocGia == dg.MaDocGia);
                    docgia.HoTen = dg.HoTen;
                    docgia.GioiTinh = dg.GioiTinh;
                    docgia.NgaySinh = dg.NgaySinh;
                    docgia.NgayLapThe = dg.NgayLapThe;
                    docgia.TrangThai = dg.TrangThai;
                    docgia.Phats = dg.Phats;
                    docgia.PhieuMuons = dg.PhieuMuons;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }
        public ActionResult CapNhatDGTheoTT(string maDG, string tt)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    var docgia = db.DocGias.Single(s => s.MaDocGia == maDG);
                    docgia.TrangThai = tt;
                    docgia.NgayLapThe = DateTime.Now;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }
        public ActionResult Xoa(string ms)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    var docgia = db.DocGias.Single(s => s.MaDocGia == ms);
                    db.DocGias.Remove(docgia);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }
        public ActionResultAsList<DocGia> TimKiemTheoTen(string ten)
        {
            var ar = new ActionResultAsList<DocGia>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var list = db.DocGias.Where(s => s.HoTen.Contains(ten));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<DocGia> TimKiemTheoMa(string ma)
        {
            var ar = new ActionResultAsList<DocGia>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var list = db.DocGias.Where(s => s.MaDocGia.Contains(ma));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<DocGia> TimKiemTheoTrangThai(string trangthai)
        {
            var ar = new ActionResultAsList<DocGia>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var list = db.DocGias.Where(s => s.TrangThai.Contains(trangthai));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResult KhoaThe(string ma)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    var docgia = db.DocGias.Single(s => s.MaDocGia == ma);
                    docgia.TrangThai = "Khóa thẻ";
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }
        public ActionResultAsList<DocGia> LayDGTheoTrangThai(string trangThai)
        {
            var ar = new ActionResultAsList<DocGia>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    ar.ObjectList = db.DocGias.Where(s => s.TrangThai == trangThai).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<DocGia> LayDSDocGiaLapThe(int thang, int nam)
        {
            var ar = new ActionResultAsList<DocGia>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    ar.ObjectList = db.DocGias.Where(p => p.NgayLapThe.Value.Month == thang && p.NgayLapThe.Value.Year == nam).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
    }
}
